#!/usr/bin/python3
# -*- coding: utf-8 -*-

eps = 1e-8

def signal(*args):
    # Cci_v3 最常用的T指标
    df = args[0]
    n  = args[1]
    factor_name = args[2]

    oma = df['open'].ewm(span=n, adjust=False).mean()
    hma = df['high'].ewm(span=n, adjust=False).mean()
    lma = df['low'].ewm(span=n, adjust=False).mean()
    cma = df['close'].ewm(span=n, adjust=False).mean()
    tp = (oma + hma + lma + cma) / 4
    ma = tp.ewm(span=n, adjust=False).mean()
    md = (cma - ma).abs().ewm(span=n, adjust=False).mean()
    df[factor_name] = (tp - ma) / (md + eps)

    return df
